package com.yuanlp.shoppingmall.home.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.yuanlp.shoppingmall.R;
import com.yuanlp.shoppingmall.base.BaseFragement;
import com.yuanlp.shoppingmall.home.adapter.HomeFragmentAdapter;
import com.yuanlp.shoppingmall.home.model.ResultBeanData;
import com.yuanlp.shoppingmall.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

import static android.content.ContentValues.TAG;

/**
 * Created by 原立鹏 on 2017/8/8.
 * 主页面的Fragment
 */

@SuppressLint("ValidFragment")
public class HomeFragment extends BaseFragement{
    private TextView tv_search_home;
    private TextView tv_message_home;
    private RecyclerView rv_home;
    private ImageButton ib_top;
    private ResultBeanData.ResultBean resultBean;
    private HomeFragmentAdapter adapter;

    @SuppressLint("ValidFragment")
    public HomeFragment(Context context) {

    }

    /**
     * 强制子类实现他，让在oncreateview里显示这个布局。
     *
     * @return
     */
    @Override
    public View initview() {

        View view=View.inflate(context, R.layout.fragement_home,null);
        tv_search_home= (TextView) view.findViewById(R.id.tv_search_home);
        tv_message_home= (TextView) view.findViewById(R.id.tv_message_home);
        rv_home= (RecyclerView) view.findViewById(R.id.rv_home);
        ib_top= (ImageButton) view.findViewById(R.id.ib_top);

        //设置监听
        initListener();
        return view;
    }

    private void initListener() {
        //设置置顶按钮监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rv_home.scrollToPosition(0);

            }
        });

        tv_search_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击了搜索",Toast.LENGTH_SHORT).show();
            }
        });

        tv_message_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"点击了消息",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        System.out.println("开始加载数据--------------------------");
        getDataFromNet();
    }

    private void getDataFromNet() {
        String url = Constants.HOME_URL;
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback()
                {
                    /**
                     * 联网失败，调用此方法
                     * @param call
                     * @param e
                     * @param id
                     */
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e(TAG, "onError: 首页失败时返回错误=="+e.getMessage());
                    }

                    /**
                     * 联网成功，调用此方法
                     * @param response
                     * @param id
                     */
                    @Override
                    public void onResponse(String response, int id) {

                        Log.d(TAG, "onResponse: "+response);
                        /**
                         * 解析json字符串
                         */
                        processData(response);
                    }
                });

    }

    /**
     * 解析字符串
     * @param json
     */
    private void processData(String json) {
        ResultBeanData result = (ResultBeanData) JSON.parseObject(json,ResultBeanData.class);
        resultBean = result.getResult();
        if (resultBean!=null){
            //将数据传给bean
            adapter=new HomeFragmentAdapter(context,resultBean);
            rv_home.setAdapter(adapter);
            GridLayoutManager manager=new GridLayoutManager(context,1);

            //gridlayoutmanager有一个设置跨度大小的监听
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if(position <= 3){
                        //隐藏
                        ib_top.setVisibility(View.GONE);
                    }else{
                        //显示
                        ib_top.setVisibility(View.VISIBLE);
                    }
                    //只能返回1
                    return 1;
                }
            });

            //给recycleview设置布局管理者
            rv_home.setLayoutManager(manager);
        }

    }
}
